package com.shanghe.house.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shanghe.house.common.Result;
import com.shanghe.house.dto.DeviceDTO;
import com.shanghe.house.entity.Device;
import com.shanghe.house.service.DeviceService;

import jakarta.validation.Valid;

/**
 * 设备控制器
 * 处理设备相关的HTTP请求，提供RESTful API接口
 */
@RestController
@RequestMapping("/api/devices")
public class DeviceController {
    private final DeviceService deviceService;

    public DeviceController(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @GetMapping
    public Result<List<Device>> findAll() {
        return Result.success(deviceService.findAll());
    }

    @GetMapping("/{id}")
    public Result<Device> findById(@PathVariable Long id) {
        return Result.success(deviceService.findById(id));
    }

    @PostMapping
    public Result<Device> create(@Valid @RequestBody DeviceDTO deviceDTO) {
        return Result.success(deviceService.create(deviceDTO));
    }

    @PutMapping("/{id}")
    public Result<Device> update(@PathVariable Long id, @Valid @RequestBody DeviceDTO deviceDTO) {
        return Result.success(deviceService.update(id, deviceDTO));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        deviceService.delete(id);
        return Result.success(null);
    }

    @GetMapping("/search")
    public Result<List<Device>> search(@RequestParam String keyword) {
        return Result.success(deviceService.search(keyword));
    }
} 